$(function () {
    $('#dg').datagrid({
        url: $WEB_ROOT_PATH + '/role/list',
        singleSelect: true,//单选
        height: window.innerHeight - 48,
        // headers: {
        //     //'tokenAz': $TokenUtil.getToken()
        // },
        headers: $TokenHeaders,
        onLoadSuccess: function (data) {
            //console.log(data);
        },
        onLoadError: function (res) {
            //console.log(res.responseJSON);
            $TokenReUtil.reLogin();
        },
        columns: [[
            {field: '', checkbox: true, sortable: false, width: 100},
            // {field: 'id', title: 'id', width: 100},
            {field: 'roleName', title: '角色名称', width: 100},
            {field: 'roleEn', title: '角色标识', width: 100}
            // {field: 'isEnabled', title: '标识', formatter: isEnabled, width: 100}
        ]]
    });

    /*function isEnabled(val, row) {
        if (val === '0') {
            return "正常"
        } else {
            return "禁用"
        }
    }*/

    //关闭dialog
    $('#closeId').click(function () {
        $("#dialogId").dialog("close");
        //var queryParams = $('#dg').datagrid('options').queryParams; //在此基础可进行模糊查询
        //queryParams.name = '李四'; //如，依次添加属性
        $("#dg").datagrid("reload"); //刷新
    });

    //编辑角色
    $("#saveId").click(function () {
        if ($("#fromId").form('validate')) {
            $.ajaxAsyncJson($WEB_ROOT_PATH + '/role/save', 'post', $("#fromId").serializeJsonStr(), function (res) {
                if (res.code === 0) {
                    /*$.messager.alert('提示',res.data,"",function(){
                        $("#dialogId").dialog("close");
                        clearForm('fromId');
                    });
                    // alert 去掉关闭按钮
                    $('.panel-tool-close').hide();*/
                    $.messager.show({
                        title: '提示',
                        msg: res.data,
                        timeout: 3000,
                        showType: 'slide'
                    });
                    $("#dialogId").dialog("close");
                    clearForm('fromId');
                } else {
                    resError(res);
                }
            }, function (res) {
            }, 'json');
        }
    });
});

/**
 * clear form reload list
 */
function clearForm(fromId) {
    $("#" + fromId).form("clear"); //清空表单
    $("#dg").datagrid("reload"); //刷新
}

/**
 * 查询
 */
function doSearch() {
    $('#dg').datagrid("load", {
        roleName: $.trim($("#roleName").val())
    });
    $("#dg").datagrid("clearChecked");
    $("#dg").datagrid("clearSelections");
}

/**
 * 添加dialog
 */
function addDialog() {
    $("#dialogId").dialog({
        closed: false,
        closable: false,
        /*left: 0,
        top: 0,*/
        title: '添加角色',
        iconCls: 'icon-save',
        onOpen: function () {
            //载入联动地址
            // openSelectAddress();
        }
    });
}

/**
 * 修改弹窗
 */
function updateDialog() {
    var selRow = $("#dg").datagrid("getSelections");// 返回选中多行
    if (selRow.length == 0 || selRow.length > 1) {
        $.messager.alert("提示", "请选择一行数据！", "warning");
        return;
    }
    var id = selRow[0].id; //标识id
    $.ajaxAsync($WEB_ROOT_PATH + '/role/detail', 'GET', {id: id}, function (res) {
        if (res.code === 0) {
            $("#dialogId").dialog({
                closed: false,
                closable: false,
                /*left:0,
                top:0,*/
                title: '修改角色',
                iconCls: 'icon-edit',
                onOpen: function () {
                    $("#fromId").form("load", res.data); //载入表单
                }
            });
        } else {
            resError(res);
        }
    }, function (res) {
    }, 'json');
}

/**
 * ajax req error
 * @param res
 */
function resError(res) {
    $.messager.alert('提示', res.data, "", function () {
    });
    $('.panel-tool-close').hide();
}

/**
 * delete success
 * @param res
 */
function resDel(res){
    $.messager.show({
        title: '提示',
        msg: res.data,
        timeout: 3000,
        showType: 'slide'
    });
    clearForm('fromId');
}

/**
 * 删除数据
 */
function del() {
    var selRow = $("#dg").datagrid("getSelections");// 返回选中多行
    if (selRow.length == 0) {
        $.messager.alert("提示", "请至少选择一行数据！", "warning");
        return;
    }
    var ids = [];
    for (var i = 0; i < selRow.length; i++) {
        ids.push(selRow[i].id);
    }
    $.messager.confirm("提示", "您确定要删除选中的数据吗？", function (data) {
        if (data) {
            var url = $WEB_ROOT_PATH + '/role/delete';
            var userName = $TokenUtil.getSub();
            $.ajaxAsync(url, 'POST', {ids: ids, userName: userName}, function (res) {
                if (res.code === 0) {
                    resDel(res);
                } else {
                    resError(res);
                }
            }, function (res) {
            }, 'json');

            // $.post(url, {"ids": ids}, function (r) {
            //     $.messager.alert('提示', '操作成功', "info", function () {
            //         $("#userListId").datagrid("reload"); //刷新
            //     });
            // });

        }
    });
}